﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace curbside.Web.UI.WebControls
{
    public class PicUpload : UserControl
    {
        Image img = new Image();
        FileUpload fupload = new FileUpload();
        HiddenField hfOPFlag = new HiddenField();
        HiddenField hfIsImg = new HiddenField();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            fupload.CssClass = "webui-picupload";
            this.Controls.Add(img);
            this.Controls.Add(fupload);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);

            string jsEvent = "$(function(){var fupload = $('#" + this.fupload.ClientID + "');";
            string jsCode = string.Empty;
            jsCode += "fupload.change(function(){alert(1);})";
            //if (!string.IsNullOrEmpty(DefaultValue))
            //{
            //    jsCode += string.Format("jtxt.attr('defaultValue','{0}');memo(jtxt.get(0));", DefaultValue);
            //}

            //if (Autocomplete)
            //{
            //    jsCode += "jtxt.autocomplete({"
            //        + "source:" + JavascriptSerializeHelper.Serialize(DataSource) + ","
            //        + "minLength:" + AutoMinLength
            //        + "})";
            //}

            if (!string.IsNullOrEmpty(jsCode))
            {
                jsEvent += jsCode + "});";
                helper.AddStartupScript(ClientID, jsEvent, this.Page);
            }
        }

        #region 属性

        /// <summary>
        /// 设置或获取图片宽度
        /// </summary>
        public Unit ImgWidth
        {
            set
            {
                this.img.Width = value;
            }
            get
            {
                return this.img.Width;
            }
        }

        /// <summary>
        /// 设置或获取图片高度
        /// </summary>
        public Unit ImgHeight
        {
            set
            {
                this.img.Height = value;
            }
            get
            {
                return this.img.Height;
            }
        }

        /// <summary>
        /// 获取图片的字节数组
        /// </summary>
        public byte[] FileBytes
        {
            get { return this.fupload.FileBytes; }
        }

        /// <summary>
        /// 获取文件尺寸(单位K) 
        /// </summary>
        public long FileSize
        {
            get { return this.fupload.FileContent.Length / 1024 + 1; }
        }

        /// <summary>
        /// 设置或获取图片的src
        /// </summary>
        public string ImgSrc
        {
            set
            {
                this.img.ImageUrl = value;
                this.hfIsImg.Value = "true";
                this.hfOPFlag.Value = "update";
            }
            get
            {
                return this.img.ImageUrl;
            }
        }

        #endregion
    }
}
